<?php
/**
 * Created by PhpStorm.
 * User: QK
 * Date: 2018-07-25
 * Time: 上午 10:21
 */
namespace app\blog\controller;
use cmf\controller\AdminBaseController;
use app\blog\model\GoodsModel as go;
class GoodsController extends AdminBaseController{
    /**
     * 商品列表页面
    */
    public function _initialize()
    {
        parent::_initialize();
        $this->go =new go();
    }

    public function indexs(){

    }
    public function index(){
        $array=$this->request->param();
        $where='';
        $cause='';
        if(isset($array['g_category']))
        {
          if($array['g_category']>0)
          {
              $where['go.g_category']=$array['g_category'];
              $cause['id']=$array['g_category'];
          }else{

          }
        }else{
            $array['g_category']='';
        }
        if(isset($array['goods_name']))
        {
            $where['go.goods_name']=array('like','%'.$array['goods_name'].'%');
        }else{
            $array['goods_name']='';
        }
        /*商品类别*/

        $category_array=$this->go->category($cause,$state=1);
        $data=$this->go->index($where);
        $data->appends(['g_category'=>$array['g_category'],'goods_name'=>$array['goods_name']]);
        $this->assign('data',$data);
        $this->assign('category',$category_array);
        $this->assign('page',$data->render());
        return $this->fetch();
    }
    /**
     * 添加商品页面
    */
    public function add(){
        $format=$this->go->format();
        $brand=$this->go->brand();
        $category_array=$this->go->category();
        $this->assign('category',$category_array);
        $this->assign('format',$format);
        $this->assign('brand',$brand);
        return $this->fetch();
    }
    /**
     * 添加商品操作
    */
    public function addpost(){
        $date = $this->request->post();
        $status=$this->go->add($date);
        $this->success($status);
    }
    /**
     * 修改商品页面
    */
    public function detail(){
        $date=$this->request->param();
        $where['go.g_id']=$date['id'];
        $state=$this->go->index($where);
        $date='';
        foreach ($state as $k=>$v)
        {
            $date=$v;
        }
        $format=$this->go->format();
        $brand=$this->go->brand();
        $category_array=$this->go->category();
        $this->assign('category',$category_array);
        $this->assign('format',$format);
        $this->assign('brand',$brand);
        $this->assign('date',$date);
        return $this->fetch();
    }
    /*修改页面接收参数*/
    public function detailpost()
    {
        $date=$this->request->param();
        $state=$this->go->detail($date);
        $this->success($state);
    }
    /*删除商品*/
    public function del()
    {
        $date=$this->request->param();
        $state=$this->go->del($date);
        if($state['num']==1)
        {
          return   $this->success($state['con']);
        }else{
          return  $this->error($state['con']);
        }
    }

}